package com.tech.platform.cms.authz.sso.controller;

import com.tech.platform.cms.authz.sso.exception.NoSuchClientException;
import com.tech.platform.cms.common.constant.Constant;
import com.tech.platform.cms.common.model.CommonResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletResponse;

@RestControllerAdvice
public class SsoAuthzExceptionController extends ResponseEntityExceptionHandler {

//    @ExceptionHandler(LoginException.class)
//    public void loginExceptionHandler(HttpServletResponse response, final LoginException ex) throws Exception{
//        response.sendRedirect("/loginError");
//    }

    @ExceptionHandler(NoSuchClientException.class)
    public ResponseEntity<CommonResult<String>> noSuchClientExceptionHandler(HttpServletResponse response, NoSuchClientException ex) throws Exception{
//        response.getWriter().write(JSONUtil.toJsonStr(new CommonResult<String>(Constant.AUTHORIZATIONERROR,ex.getMessage())));
        return new ResponseEntity<>(new CommonResult<String>(Constant.AUTHORIZATIONERROR,ex.getMessage()), HttpStatus.UNAUTHORIZED);
    }
}
